﻿
using Microsoft.Extensions.DependencyInjection;
namespace SB2.Core;

public static class SqlsugarSetup
{
    public static void AddSqlsugarSingleton(this IServiceCollection services)
    {
        SqlSugarScope sqlSugar = new SqlSugarScope(
            App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs"),
            db =>
            {
                //单例参数配置，所有上下文生效
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(sql);//输出sql
                };

                //技巧：拿到非ORM注入对象
                //services.GetService<注入对象>();
            });
        sqlSugar.DbMaintenance.CreateDatabase(); // 创建数据库
        services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
    }
}
